package com.vitabuybuy.tv.network

import com.library.common.net.NetUrl
import com.vitabuybuy.tv.dto.*
import rxhttp.IAwait
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

/**
 * 作者：SimGa
 * 日期：2020/12/3
 * 时间：17:04
 * 包名：com.vitabuybuy.tv.network
 * desc 是倒序，是从高到低的排序
 * asc 是正序，从低到高的排序
 */
object ShopCenterRepository {

    fun getFirstCategory(): IAwait<List<FirstCategoryDto>> {
        return RxHttp.postJson(NetUrl.api_shop_first_category)
            .add("category", mapOf(Pair("type", "Product")))
            .toResponse()
    }

    fun getAllBrand(): IAwait<MutableList<BrandDto>> {
        return RxHttp.postJson(NetUrl.api_all_brand).addAll("{}").toResponse()
    }

    fun gethotKey(): IAwait<MutableList<HotKeyDto>> {
        return RxHttp.postJson(NetUrl.api_hot_key).addAll("{}").toResponse()
    }

    fun getClassification(): IAwait<MutableList<ClassificationDto>> {
        return RxHttp.postJson(NetUrl.api_all_cate)
            .add("type", "Product").toResponse()
    }

    fun getHomeGoodList(
        id: Int,
        sort: String,
        pageNumber: Int,
        pageSize: Int
    ): IAwait<List<GoodsDto>> {
        val pageMap = HashMap<String, Any>()
        pageMap["page"] = pageNumber
        pageMap["size"] = pageSize
        when (sort) {
            "new" -> {
                pageMap["desc"] = "id"
            }
            "up_price" -> {
                pageMap["asc"] = "sale_price"
            }
            "down_price" -> {
                pageMap["desc"] = "sale_price"
            }
        }
        val params = HashMap<String, Any>()
        params["pageable"] = pageMap
        if (id != -1) {
            params["category"] = mapOf(Pair("id", id))
        }
        if (sort == "featured") {
            params["product"] = mapOf(Pair("featured", true))
        }
        return RxHttp.postJson(if (id != -1) NetUrl.api_home_shop_goods_by_first else NetUrl.api_home_shop_goods)
            .addAll(params).toResponse()
    }

    fun getCateSearchGoodList(
        content: Any,
        storeId: Int?,
        couponId: Int?,
        sort: String,
        pageNumber: Int,
        pageSize: Int
    ): IAwait<List<GoodsDto>> {
        var apiStr = ""
        apiStr = if (storeId != null && storeId != 0) {
            if (content is String) {
                NetUrl.api_home_shop_goods
            } else {
                NetUrl.api_find_by_store_cate
            }
        } else {
            NetUrl.api_home_shop_goods
        }
        val params = HashMap<String, Any>()
        if (storeId != null && storeId != 0) {
            when (content) {
                is String -> {
                    val productMap = HashMap<String, Any>()
                    productMap["name"] = content
                    if (sort == "featured") {
                        productMap["featured"] = true
                    }
                    params["product"] = productMap
                }
                is Int -> if (content != 0 && content != -1) params["category"] =
                    mapOf(Pair("id", content))
            }
            params["storeId"] = storeId
        } else {
            when (content) {
                is String -> {
                    val productMap = HashMap<String, Any>()
                    if (couponId == null || couponId == 0) productMap["name"] = content
                    if (sort == "featured") {
                        productMap["featured"] = true
                    }
                    params["product"] = productMap
                }
                is Int -> if (content != 0 && content != -1) params["category"] =
                    mapOf(Pair("id", content))
            }
            if (couponId != null && couponId != 0) params["coupon"] = mapOf(Pair("id", couponId))
        }
        val pageMap = HashMap<String, Any>()
        pageMap["page"] = pageNumber
        pageMap["size"] = pageSize
        when (sort) {
            "new" -> {
                pageMap["desc"] = "id"
            }
            "up_price" -> {
                pageMap["asc"] = "sale_price"
            }
            "down_price" -> {
                pageMap["desc"] = "sale_price"
            }
        }
        params["pageable"] = pageMap
        return RxHttp.postJson(apiStr).addAll(params).toResponse()
    }

    fun getScreenGoodList(
        brandList2: MutableList<BrandDto>,
        salePriceDto: SalePriceDto,
        sort: String,
        AnyValue: Any,
        pageNumber: Int,
        pageSize: Int,
        regionId: Int
    ): IAwait<List<GoodsDto>> {
        val map = HashMap<String, Any>();
        val pageMap = HashMap<String, Any>()
        pageMap["page"] = pageNumber
        pageMap["size"] = pageSize
        when (sort) {
            "new" -> {
                pageMap["desc"] = "id"
            }
            "up_price" -> {
                pageMap["asc"] = "sale_price"
            }
            "down_price" -> {
                pageMap["desc"] = "sale_price"
            }
        }
        if (regionId != -1) {
            map["region"] = mapOf(Pair("id", regionId))
        }
        map["pageable"] = pageMap
        if(salePriceDto.start != 0.0 && salePriceDto.end != 0.0){
            map["salePrice"] = salePriceDto
        }
        map["brandList"] = brandList2
        val params = HashMap<String, Any>()
        when (AnyValue) {
            is Int -> {
                if (AnyValue != -1) {
                    map["category"] = mapOf(
                        Pair("id", AnyValue)
                    )
                }
                if (sort == "featured") {
                    params["featured"] = true
                    map["product"] = params
                }
            }
            is String -> {
                if (sort == "featured") {
                    params["featured"] = true
                }
                params["name"] = AnyValue
                map["product"] = params
            }
        }
        return RxHttp.postJson(NetUrl.api_home_shop_goods).addAll(map).toResponse()
    }


    fun getDetails(goodId: Int?): IAwait<GoodsDetailsDto> {
        return RxHttp.postJson(NetUrl.api_get_goods_details)
            .add("id", goodId).toResponse()
    }

    fun addToCart(goodId: Int?, number: Int): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_add_to_cart)
            .add("goods", mapOf(Pair("id", goodId)))
            .add("number", number)
            .toResponse()
    }

    fun searchStoreList(content: String, pageNumber: Int): IAwait<List<StoreListDto>> {
        val params = mapOf(
            Pair(
                "pageable",
                mapOf(
                    Pair("page", pageNumber),
                    Pair("size", 10),
                    Pair("desc", "id")
                )
            ),
            Pair(
                "store", mapOf(
                    Pair("name", content)
                )
            )
        )
        return RxHttp.postJson(NetUrl.api_search_stroe).addAll(params).toResponse()
    }

    fun collectOrcancel(id: Int, isFav: Boolean): IAwait<String> {
        return if (!isFav) {
            RxHttp.postJson(NetUrl.api_collect_good_or_store)
                .add("favorites", mapOf(Pair("product", mapOf(Pair("id", id)))))
                .toResponse()
        } else {
            RxHttp.postJson(NetUrl.api_cancel_collect_good_or_store)
                .add("product", mapOf(Pair("id", id)))
                .toResponse()
        }
    }

    fun getGoodsCommentList(
        goodId: Int?,
        isImage: Boolean,
        pageNumber: Int,
        pageSize: Int
    ): IAwait<MutableList<GoodsCommentDto>> {
        val params = HashMap<String, Any>()
        params["product"] = mapOf(Pair("id", goodId))
        params["images"] = isImage
        params["pageable"] = mapOf(
            Pair("page", pageNumber),
            Pair("size", pageSize),
            Pair("desc", "id")
        )
        return RxHttp.postJson(NetUrl.api_goods_comment_list).addAll(params).toResponse()
    }

    fun getGoodsRecommendAndSimilerList(
        goodId: Int?
    ): IAwait<GoodsRecommendAndSimilerDto> {
        val map = HashMap<String, Any?>()
        map["id"] = goodId
        return RxHttp.postJson(NetUrl.api_goods_recommend_and_similer)
            .add("id", goodId).toResponse()
    }
}